package web.department;

import bean.Department;
import bean.Type;
import com.alibaba.fastjson.JSON;
import entity.DepartmentEntity;
import util.FANREN;

import javax.persistence.EntityManager;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "DepartmentEdit",urlPatterns = "/dao/update/departmentEdit")
public class DepartmentEdit extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String jsonString = FANREN.getJson(request);
        Department department = JSON.parseObject(jsonString,Department.class);
        EntityManager em = (EntityManager) request.getAttribute("_entity.Manager");
        DepartmentEntity departmentEntity = null;
        if(department.getDid().equals("")) {
            //新增
            departmentEntity = new DepartmentEntity();
        } else {
            //修改
            departmentEntity = em.find(DepartmentEntity.class,Integer.parseInt(department.getDid()));
        }
        departmentEntity.setName(department.getName());
        em.persist(departmentEntity);
    }
}
